﻿using MediatR;
using SY.BasicData.Api.Read.Applications.DeepProcessing.Command;
using SY.BasicData.Domain.DeepProcessing;
using SY.BasicData.ErrorCode;
using SY.BasicData.Infrastructures;

namespace SY.BasicData.Api.Read.Applications.DeepProcessing.CommandHandler
{
    public class DeepProcessingCommandHandler : IRequestHandler<DeepProcessingCommand, ApiResult<DeepProcessingModel>>
    {
        private readonly IBaseRepository<DeepProcessingModel> deepProcessRepository;

        public DeepProcessingCommandHandler(IBaseRepository<DeepProcessingModel> deepProcessRepository)
        {
            this.deepProcessRepository = deepProcessRepository;
        }

        public Task<ApiResult<DeepProcessingModel>> Handle(DeepProcessingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<DeepProcessingModel> result = new ApiResult<DeepProcessingModel>();
            result.Code = ApiEnum.Success;
            result.Message = "深加工信息明细成功";
            var list = deepProcessRepository.GetAll().Where(x => x.Batch == request.Batch).FirstOrDefault();
            if (list == null)
            {
                result.Code = ApiEnum.Fail;
                result.Message = "深加工信息明细失败";
                return Task.FromResult(result);
            }
            result.Data = list;
            return Task.FromResult(result);
        }
    }
}
